<?php
/**
 * Created by PhpStorm.
 * User: 六花
 * Date: 2019/4/27
 * Time: 17:20
 */

namespace Api\Controller;

use Think\Controller;
use Think\Log;


class OrderController extends Controller {

    // log日志
    private $logFile;

    // 构造函数
    public function  __construct()
    {
        $this->logFile = C('LOG_PATH') . 'Api_order_' . date('y_m_d') . '.log';
    }

    public function edit(){
        $spec_model = M('goods_spec');
        if(!$_POST['Item'] || !isset($_POST['Item'])){
            $return_array = array(
                'MessageCode' => 2,
                'Message' => '参数错误'
            );
            return json_encode($return_array);
        }
        $spec_model->startTrans();
        foreach($_POST['Item'] as $k => $v){
            if(!$v['sku'] || !$v['Count']){
                $spec_model -> rollback();
                $return_array = array(
                    'MessageCode' => 2,
                    'Message' => '参数错误'
                );
                return json_encode($return_array);
            }

            $res = $spec_model -> where('sku=' . $v['sku']) ->find();

            if(!$res){
                $spec_model -> rollback();
                $return_array = array(
                    'MessageCode' => 2,
                    'Message' => '参数错误'
                );
                return json_encode($return_array);
            }

            $stock = intval($v['stock']) + intval($res['stock']);

            $resSave = $spec_model -> where('sku=' . $v['sku']) ->save(array('stock'=>$stock));
        }

        $spec_model -> commit();
        $return_array = array(
            'MessageCode' => 1,
            'Message' => '修改成功'
        );
        return json_encode($return_array);
    }

    public function editOrder(){
//        $_POST = json_decode('{
//            "ClientGuid": "string",
//  "ClientName": "string",
//  "OrderBn": "1912412659",
//  "SubOrderBn": "1912412659",
//  "OrderItems": [
//    {
//        "Sku": "asa01001",
//      "OrderStatus": 62,
//      "SkuItems": [
//        {
//            "Sku": "asa01001",
//          "OrderStatus": 62
//        }
//      ]
//    }
//  ]
//}',true);
//        $_POST = $GLOBALS['HTTP_RAW_POST_DATA'];
//        echo json_decode($_POST,true);exit;
//        $_POST = json_decode('{"ID":0,"ClientGuid":"e49d8e19-cce4-4da1-bf07-ee2c9c2dca5c","ClientName":"ASA-OMS","OrderBn":null,"SubOrderBn":"1912412659","OrderItems":[{"Sku":"asa01001","OrderStatus":20,"SkuItems":[{"Sku":"asa01001","OrderStatus":20}]}]}',true);

        // 添加日志，记录原始接收到的OMS发送过来的数据，Powered by liuzongyang 2019/10/9 09:57
        $message = '现在正在记录OMS发送过来的editOrder Request的数据，请知悉： ' . PHP_EOL;
        $message .= 'OMS发送过来的editOrder Request的信息如下: ' . PHP_EOL;
        $message .= print_r($GLOBALS['HTTP_RAW_POST_DATA'], true);
        Log::write($message, 'debug', '', $this->logFile);

        $_POST = json_decode($GLOBALS['HTTP_RAW_POST_DATA'],true);
        $goods_model = M('goods');
        $goods_spec_model = M('goods_spec');
        $order_model = M('order');
        $order_goods_model = M('order_goods');
        if(!$_POST['OrderItems']){
            $return_array = array(
                'MessageCode' => 2,
                'Message' => '参数错误1'
            );
            echo json_encode($return_array,JSON_UNESCAPED_UNICODE);exit;
        }
        $goods_model->startTrans();

        $res_order = $order_model -> where(array('order_sn' =>$_POST['SubOrderBn']))->find();


        foreach($_POST['OrderItems'] as $key => $value) {
            if (!isset($value['SkuItems']) || !$value['SkuItems']) {
                $goods_model->rollback();
                $return_array = array(
                    'MessageCode' => 2,
                    'Message' => '参数错误2'
                );

                echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
                exit;
            }
            if($value['OrderStatus'] == 92 || $value['OrderStatus'] == 102){
                if(!$res_order['express_no']){
                    $data['express_name'] = $_POST['ShipmentCopName'];      //快递公司名称
                    $data['express_code'] = $_POST['ShipmentCopCode'];      //快递公司编码
                    $data['express_no'] = $_POST['TrackingNo'];             //快递单号
                }

            }
            if ($value['OrderStatus'] == 90 || $value['OrderStatus'] == 91 || $value['OrderStatus'] == 93 || $value['OrderStatus'] == 95) {
                $data['status'] = 20;
                $data['customs_status'] = $value['OrderStatus'];
                $res = $order_model->where(array('order_sn' =>$_POST['SubOrderBn']))->save($data);
                if($res){
                    $goods_model->commit();
                    $return_array = array(
                        'MessageCode' => 1,
                        'Message' => '更改待发货状态成功'
                    );
                    echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
                    exit;
                }else{
                    $goods_model->rollback();
                    $return_array = array(
                        'MessageCode' => 2,
                        'Message' => '更改待发货状态失败'
                    );
                    echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
                    exit;
                }
            }else if ($value['OrderStatus'] == 92 || $value['OrderStatus'] == 96 || $value['OrderStatus'] == 102) {
                $data['status'] = 30;
                $data['customs_status'] = $value['OrderStatus'];
                $data['auto_finished_time'] = time() + 15 * 24 * 3600;

                $res = $order_model->where(array('order_sn' =>$_POST['SubOrderBn']))->save($data);
                if($res){
                    $goods_model->commit();
                    $return_array = array(
                        'MessageCode' => 1,
                        'Message' => '更改已发货状态成功'
                    );
                    echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
                    exit;
                }else{
                    $goods_model->rollback();
                    $return_array = array(
                        'MessageCode' => 2,
                        'Message' => '更改已发货状态失败'
                    );
                    echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
                    exit;
                }
            }else if ($value['OrderStatus'] == 20) {
                $data['status'] = 0;
                $data['customs_status'] = $value['OrderStatus'];
                $res = $order_model->where(array('order_sn' =>$_POST['SubOrderBn']))->save($data);
                if($res){
                    $goods_model->commit();
                    $return_array = array(
                        'MessageCode' => 1,
                        'Message' => '取消订单成功'
                    );
                    echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
                    exit;
                }else{
                    $goods_model->rollback();
                    $return_array = array(
                        'MessageCode' => 2,
                        'Message' => '取消订单失败'
                    );
                    echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
                    exit;
                }
            } else {
                $data['customs_status'] = $value['OrderStatus'];
                $return_array = array(
                    'MessageCode' => 2,
                    'Message' => '更改其他状态成功'
                );
                echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
                exit;
            }
            //成功
//            foreach ($_POST['OrderItems'][$k]['SkuItems'] as $key => $value) {
//                $temp_data = null;
//                $temp_data['sku'] = $value['Sku'];
//                $res_spec = $goods_spec_model->where($temp_data)->find();
//                if (!$res_spec) {
//                    $return_array = array(
//                        'MessageCode' => 2,
//                        'Message' => '订单未找到'
//                    );
//                    echo json_encode($return_array,JSON_UNESCAPED_UNICODE);
//                    exit;
//                }
//                $temp_data = null;
//                $temp_data['spec_id'] = $res_spec['spec_id'];
//                $temp_data['order_id'] = $res_order['order_id'];
//                if ($value['OrderStatus'] == 62) {
//                    $data['customs_status'] = 2;
//                    $res = $order_goods_model->where($temp_data)->save($data);
////                    var_dump($order_goods_model->getLastSql());exit;
//                } else if ($value['OrderStatus'] == 81) {
//                    $data['customs_status'] = 1;
//                    $res = $order_goods_model->where($temp_data)->save($data);
//                } else if ($value['OrderStatus'] == 82 || $value['OrderStatus'] == 90|| $value['OrderStatus'] == 91 || $value['OrderStatus'] == 92 || $value['OrderStatus'] == 93 || $value['OrderStatus'] == 94 || $value['OrderStatus'] == 95 || $value['OrderStatus'] == 96 || $value['OrderStatus'] == 100) {
//                    $data['customs_status'] = 3;
//                    $res = $order_goods_model->where($temp_data)->save($data);
//                if ($value['OrderStatus'] != 20) {
//                    $data['customs_status'] = $value['OrderStatus'];
//                    $res = $order_goods_model->where($temp_data)->save($data);
//                }

            }
//        }
//            $temp_data = null;
//            $temp_data['order_id'] = $res_spec['order_id'];
//
//            $res_sub = $order_goods_model -> where($temp_data)->select();
//            //全部成功
//            $is_success = true;
//            //全部失败
//            $is_error = true;
//            foreach ($res_sub as  $k1 => $v1) {
//                if($v1['customs_status'] == 2 || $v1['customs_status'] == 3){
//                    $is_success = false;
//                }
//                if($v1['customs_status'] == 1){
//                    $is_error = false;
//                }
//            }
//            if($is_success){
//                $data_order['customs_status'] = 1;
//            }else if($is_error){
//                $data_order['customs_status'] = 2;
//            }else if(!$is_success && !$is_error){
//                $data_order['customs_status'] = 3;
//            }
//
//        $temp_data = null;
//        $temp_data['order_sn'] = $_POST['SubOrderBn'];
//
//            $res_order = $order_model -> where(array('order_sn'=>$_POST['SubOrderBn']))->save($data_order);
//
//            if($res_order === false){
//                $goods_model->rollback();
//                $return_array = array(
//                    'MessageCode' => 2,
//                    'Message' => '更新失败'
//                );
//                echo json_encode($return_array,JSON_UNESCAPED_UNICODE);exit;
//            }else{
//                $goods_model->commit();
//                $return_array = array(
//                    'MessageCode' => 1,
//                    'Message' => '更新成功'
//                );
//                echo json_encode($return_array,JSON_UNESCAPED_UNICODE);exit;
//
//            }


    }

    public function object_array($array) {
        if(is_object($array)) {
            $array = (array)$array;
        } if(is_array($array)) {
            foreach($array as $key=>$value) {
                $array[$key] = object_array($value);
            }
        }
        return $array;
    }
}
